package com.muse.sentinel.handler;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;

/**
 * 自定义URL限流异常
 */
@Service
public class MuseBlockHandler implements UrlBlockHandler {
    @Override
    public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                        BlockException e) throws IOException {
        httpServletResponse.setHeader("Content-Type", "application/json;charset=UTF-8");
        String message = "{\n"
                + "\t\"code\":\"400\",\n"
                + "\t\"message\":\"请求已被限流！\"\n"
                + "}";
        httpServletResponse.getWriter().write(message);
    }
}
